package com.xthh.sql;

import android.app.Application;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.os.Environment;
import android.util.Log;

import com.xthh._enum.HH_EnumBindDeviceINI;
import com.xthh._enum.HH_EnumDeviceModelINI;
import com.xthh._enum.HH_EnumLayerInfoINI;
import com.xthh._enum.HH_EnumReleaseRecordINI;

import java.io.File;


/**
 * 数据库帮助类，用于创建和管理数据库
 * */
public class HH_XTSQLiteOpenHelper extends android.database.sqlite.SQLiteOpenHelper {

    private static final String TAG = "XTSQLiteOpenHelper";


    private static final String DB_NAME = "xthh.db";

    /**
     * 构造方法的四个参数
     * 1.上下文
     * 2.数据库的名称
     * 3.游标工程
     * 4.数据库的版本，从1开始
     * */
    public HH_XTSQLiteOpenHelper(Context context, String name,
                                 CursorFactory factory, int version) {
        super(context, DB_NAME, null, 2);
    }

    public HH_XTSQLiteOpenHelper(Context context, Application application) {
        super(new HH_CustomPathDatabaseContext(context, getDirPath(application)), DB_NAME, null, 2);
    }

    /**
     * 获取db文件在sd卡的路径
     * @return
     */
    private static String getDirPath(Application application){
        String packageName = application.getPackageName();
        packageName = packageName.replaceAll("\\.", "_");
        String rootDataDir = Environment.getExternalStorageDirectory().getPath();
        rootDataDir = rootDataDir + File.separator + packageName;
        return rootDataDir;
    }

    /**
     * 数据库第一次创建的时，回调此方法
     * 初始化一些表
     * */
    @Override
    public void onCreate(SQLiteDatabase db) {
        String strParams = "";
        strParams += (HH_EnumReleaseRecordINI.tasktype.toString() + " varchar(20),");
        strParams += (HH_EnumReleaseRecordINI.picpath.toString() + " varchar(512),");
        strParams += (HH_EnumReleaseRecordINI.picpath_B.toString() + " varchar(512),");
        strParams += (HH_EnumReleaseRecordINI.tagname.toString() + " varchar(20),");
        strParams += (HH_EnumReleaseRecordINI.res.toString() + " varchar(20),");
        strParams += (HH_EnumReleaseRecordINI.color.toString() + " varchar(20),");
        strParams += (HH_EnumReleaseRecordINI.taskstate.toString() + " varchar(20),");
        strParams += (HH_EnumReleaseRecordINI.time.toString() + " varchar(20),");
        strParams += (HH_EnumReleaseRecordINI.logpath.toString() + " varchar(512),");
        strParams += (HH_EnumReleaseRecordINI.lastlog.toString() + " varchar(512),");
        strParams += (HH_EnumReleaseRecordINI.mac.toString() + " varchar(20),");
        strParams += (HH_EnumReleaseRecordINI.taskID.toString() + " varchar(20),");
        strParams += (HH_EnumReleaseRecordINI.msg_title.toString() + " varchar(64),");
        strParams += (HH_EnumReleaseRecordINI.msg_content.toString() + " varchar(512),");
        strParams += (HH_EnumReleaseRecordINI.led_control_type.toString() + " varchar(20),");
        strParams += (HH_EnumReleaseRecordINI.led_light_type.toString() + " varchar(20),");
        strParams += (HH_EnumReleaseRecordINI.led_duration_second.toString() + " varchar(20),");
        strParams += (HH_EnumReleaseRecordINI.otaPath.toString() + " varchar(512),");
        strParams += (HH_EnumReleaseRecordINI.msgTaskID.toString() + " varchar(20),");
        strParams += (HH_EnumReleaseRecordINI.msgTaskFontType.toString() + " varchar(20),");
        strParams += (HH_EnumReleaseRecordINI.msgTaskMsgType.toString() + " varchar(20)");
        //操作数据库
        String sql =  String.format("create table %s(%s);", HH_SheetName.SQL_SHEET_NAME_RELEASE_RECORD, strParams);

        db.execSQL(sql);

        strParams = "";
        strParams += (HH_EnumBindDeviceINI.proType.toString() + " varchar(20),");
        strParams += (HH_EnumBindDeviceINI.name.toString() + " varchar(512),");
        strParams += (HH_EnumBindDeviceINI.macRes.toString() + " varchar(20),");
        strParams += (HH_EnumBindDeviceINI.connectState.toString() + " varchar(20),");
        strParams += (HH_EnumBindDeviceINI.dbm.toString() + " varchar(20),");
        strParams += (HH_EnumBindDeviceINI.sel.toString() + " varchar(20),");
        strParams += (HH_EnumBindDeviceINI.picPath_A.toString() + " varchar(512),");
        strParams += (HH_EnumBindDeviceINI.picPath_B.toString() + " varchar(512),");
        strParams += (HH_EnumBindDeviceINI.bwReversal.toString() + " varchar(20),");
        strParams += (HH_EnumBindDeviceINI.bz.toString() + " varchar(512),");
        strParams += (HH_EnumBindDeviceINI.deviceColorType.toString() + " varchar(20),");
        strParams += (HH_EnumBindDeviceINI.picIndex.toString() + " varchar(20),");
        strParams += (HH_EnumBindDeviceINI.picAttr.toString() + " varchar(20),");
        strParams += (HH_EnumBindDeviceINI.ver.toString() + " varchar(20),");
        strParams += (HH_EnumBindDeviceINI.width.toString() + " varchar(20),");
        strParams += (HH_EnumBindDeviceINI.height.toString() + " varchar(20),");
        strParams += (HH_EnumBindDeviceINI.size.toString() + " varchar(20),");
        strParams += (HH_EnumBindDeviceINI.batteryB.toString() + " varchar(20),");
        strParams += (HH_EnumBindDeviceINI.onlineState.toString() + " varchar(20),");
        strParams += (HH_EnumBindDeviceINI.isWithNewspush.toString() + " varchar(20),");
        strParams += (HH_EnumBindDeviceINI.isWithFrontLight.toString() + " varchar(20),");
        strParams += (HH_EnumBindDeviceINI.isWithLED.toString() + " varchar(20),");
        strParams += (HH_EnumBindDeviceINI.rotate_A.toString() + " varchar(20),");
        strParams += (HH_EnumBindDeviceINI.rotate_B.toString() + " varchar(20),");
        strParams += (HH_EnumBindDeviceINI.chipType.toString() + " varchar(20),");
        strParams += (HH_EnumBindDeviceINI.sendPower.toString() + " varchar(20),");
        strParams += (HH_EnumBindDeviceINI.hardwareVer.toString() + " varchar(20),");
        strParams += (HH_EnumBindDeviceINI.deviceName.toString() + " varchar(20),");
        strParams += (HH_EnumBindDeviceINI.contraryROW.toString() + " varchar(20),");
        strParams += (HH_EnumBindDeviceINI.contraryCOL.toString() + " varchar(20),");
        strParams += (HH_EnumBindDeviceINI.keyCount.toString() + " varchar(20),");
        strParams += (HH_EnumBindDeviceINI.deviceNumber.toString() + " varchar(20),");
        strParams += (HH_EnumBindDeviceINI.deviceType.toString() + " varchar(20)");

        //操作数据库
        sql = String.format("create table %s(%s);", HH_SheetName.SQL_SHEET_NAME_BIND_DEVICE, strParams);
        db.execSQL(sql);

        // 模板
        strParams = "";
        strParams += (HH_EnumDeviceModelINI.deviceColorType.toString() + " varchar(12),");
        strParams += (HH_EnumDeviceModelINI.picPath.toString() + " varchar(256),");
        strParams += (HH_EnumDeviceModelINI.currentLayerIndex.toString() + " varchar(12),");
        strParams += (HH_EnumDeviceModelINI.w.toString() + " varchar(12),");
        strParams += (HH_EnumDeviceModelINI.h.toString() + " varchar(12),");
        strParams += (HH_EnumDeviceModelINI.modelID.toString() + " varchar(64),");
        strParams += (HH_EnumDeviceModelINI.modelName.toString() + " varchar(64),");
        strParams += (HH_EnumDeviceModelINI.bkgColor_r.toString() + " varchar(12),");
        strParams += (HH_EnumDeviceModelINI.bkgColor_g.toString() + " varchar(12),");
        strParams += (HH_EnumDeviceModelINI.bkgColor_b.toString() + " varchar(12),");
        strParams += (HH_EnumDeviceModelINI.isSingleColor.toString() + " varchar(12)");
        //操作数据库
        sql = String.format("create table %s(%s);", HH_SheetName.SQL_SHEET_NAME_DEVICE_MODEL, strParams);
        db.execSQL(sql);

        // 图层
        strParams = "";
        strParams += (HH_EnumLayerInfoINI.modelID.toString() + " varchar(64),");
        strParams += (HH_EnumLayerInfoINI.x.toString() + " varchar(12),");
        strParams += (HH_EnumLayerInfoINI.y.toString() + " varchar(12),");
        strParams += (HH_EnumLayerInfoINI.w.toString() + " varchar(12),");
        strParams += (HH_EnumLayerInfoINI.h.toString() + " varchar(12),");
        strParams += (HH_EnumLayerInfoINI.fontName.toString() + " varchar(64),");
        strParams += (HH_EnumLayerInfoINI.fontSize.toString() + " varchar(12),");
        strParams += (HH_EnumLayerInfoINI.font_r.toString() + " varchar(12),");
        strParams += (HH_EnumLayerInfoINI.font_g.toString() + " varchar(12),");
        strParams += (HH_EnumLayerInfoINI.font_b.toString() + " varchar(12),");
        strParams += (HH_EnumLayerInfoINI.bkg_r.toString() + " varchar(12),");
        strParams += (HH_EnumLayerInfoINI.bkg_g.toString() + " varchar(12),");
        strParams += (HH_EnumLayerInfoINI.bkg_b.toString() + " varchar(12),");
        strParams += (HH_EnumLayerInfoINI.layerName.toString() + " varchar(64),");
        strParams += (HH_EnumLayerInfoINI.isBold.toString() + " varchar(12),");
        strParams += (HH_EnumLayerInfoINI.textAlgin.toString() + " varchar(12),");
        strParams += (HH_EnumLayerInfoINI.text.toString() + " varchar(12),");
        strParams += (HH_EnumLayerInfoINI.layerAttr.toString() + " varchar(12),");
        strParams += (HH_EnumLayerInfoINI.picPath.toString() + " varchar(256),");
        strParams += (HH_EnumLayerInfoINI.picAttr.toString() + " varchar(12),");
        strParams += (HH_EnumLayerInfoINI.picSrcWidth.toString() + " varchar(12),");
        strParams += (HH_EnumLayerInfoINI.picSrcHeight.toString() + " varchar(12),");
        strParams += (HH_EnumLayerInfoINI.rotate.toString() + " varchar(12)");
        sql = String.format("create table %s(%s);", HH_SheetName.SQL_SHEET_NAME_LAYER_INFO, strParams);
        db.execSQL(sql);

    }


    /**
     * 数据库版本号更新时回调此方法
     * 更新数据库的内容(删除表，修改表，添加表)
     * */
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        if(oldVersion == 1 && newVersion == 2){ //在person表中添加余额列balance
            Log.i(TAG, "数据库更新了");

            //在person表添加一列
            //String sql = String.format("alter table %s add balance integer;", HH_SheetName.SQL_SHEET_NAME_RELEASE_RECORD);
            //db.execSQL(sql);
        }
    }
}